package com.next

import grails.converters.JSON
import grails.gorm.transactions.Transactional
import grails.validation.ValidationException
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_CONDITION_RULEENGINE'])
@Transactional(readOnly = true)
class ProuctConditionController
{

    ProuctConditionService prouctConditionService
    def componentService

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond prouctConditionService.list(params), model: [prouctConditionCount: prouctConditionService.count()]
    }

    def show(Long id)
    {
        respond prouctConditionService.get(id)
    }

    def create()
    {
        respond new ProuctCondition(params)
    }

    @Transactional
    def save(ProuctCondition prouctCondition)
    {
        if (prouctCondition == null)
        {
            notFound()
            return
        }

        try
        {
            prouctConditionService.save(prouctCondition)
        }
        catch (ValidationException e)
        {
            respond prouctCondition.errors, view: 'create'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'prouctCondition.label', default: 'ProuctCondition'), prouctCondition.id])
                redirect action: "index", method: "GET"
            }
            '*' { respond prouctCondition, [status: CREATED] }
        }
    }

    def edit(Long id)
    {
        respond prouctConditionService.get(id)
    }

    @Transactional
    def update(ProuctCondition prouctCondition)
    {
        if (prouctCondition == null)
        {
            notFound()
            return
        }

        try
        {
            prouctConditionService.save(prouctCondition)
        }
        catch (ValidationException e)
        {
            respond prouctCondition.errors, view: 'edit'
            return
        }

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'prouctCondition.label', default: 'ProuctCondition'), prouctCondition.id])
                redirect action: "index", method: "GET"
            }
            '*' { respond prouctCondition, [status: OK] }
        }
    }

    @Transactional
    def delete(Long id)
    {
        if (id == null)
        {
            notFound()
            return
        }

        prouctConditionService.delete(id)

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'prouctCondition.label', default: 'ProuctCondition'), id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'prouctCondition.label', default: 'ProuctCondition'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    @Secured(["permitAll"])
    def checkProductCpndition()
    {
        def datas = [:]
        def map = [:]
        println(params)
        map.opportunity = Opportunity.findById(params.opportunityId)
        map.product = Product.findById(params.product)
        def flag = componentService.evaluate(ProuctCondition.findByProuct(map.product)?.component, map)
        datas.code = flag
        render(datas as JSON)
    }
}
